import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  statisticList: {},
  totalList: {},
  currentStatistic: {},
  curResult: {
    complexScore: "--",
    authNum: "--",
    trainDoneRate: "--",
    taskDoneRate: "--",
    monthReadCount: "--",
    averageCount: "--",
    questCorrectRate: "--",
    newsCount: "--",
    statisticTime: "--"
  },
  searchStatisticCondition: {
    pageSize: 10,
    name: "",
    by: "trainDoneRate",
    departName: "",
    order: "desc"
  },
  searchTotalCondition: {
    pageSize: 10,
    title: ""
  },
  areaStatisList: [],
  deptStatisticList: {},
  deptRegisterRate: {},
  areaRegisterRate: []
};

const getters = {
  areaRegisterRate: state => state.areaRegisterRate,
  deptRegisterRate: state => state.deptRegisterRate,
  deptStatisticList: state => state.deptStatisticList,
  areaStatisList: state => state.areaStatisList,
  totalList: state => state.totalList,
  searchTotalCondition: state => state.searchTotalCondition,
  statisticList: state => state.statisticList,
  currentStatistic: state => state.currentStatistic,
  curResult: state => state.curResult,
  searchStatisticCondition: state => state.searchStatisticCondition
};

const actions = {
  registerStatisticAreaS({ commit }) {
    return Api.registerStatisticArea().then(ret => {
      commit(types.UPDATE_AREA_REGISTER_RATE, ret.data.data);
      return ret;
    });
  },
  registerStatisticAllS({ commit }) {
    return Api.registerStatisticAll().then(ret => {
      commit(types.UPDATE_DEPT_REGISTER_RATE, ret.data.data);
      return ret;
    });
  },
  complexDepartResultS({ commit }, param) {
    return Api.complexDepartResult(param).then(ret => {
      commit(types.UPDATE_DEPT_STATISTIC_LIST, ret.data.data);
      return ret;
    });
  },
  complexAreaResultS({ commit }, param) {
    return Api.complexAreaResult(param).then(ret => {
      commit(types.UPDATE_AREA_STATISTIC_LIST, ret.data.data);
      return ret;
    });
  },
  getPersonalStatisticPageS({ commit }, param) {
    return Api.getPersonalStatisticPage(param).then(ret => {
      commit(types.UPDATE_STATISTIC_LIST, ret.data.data);
      return ret;
    });
  },
  complexListS({ commit }, param) {
    return Api.complexList(param).then(ret => {
      commit(types.UPDATE_STATISTIC_TOTAL, ret.data.data);
      return ret;
    });
  },
  complexResultS({ commit }, payload) {
    return Api.complexResult(payload).then(ret => {
      commit(types.UPDATE_CUR_RESULT, ret.data.data);
      return ret;
    });
  },
  initResultS({ commit }) {
    commit(types.INIT_RESULTS);
  },
  deleteStatisticS({ commit }, payload) {
    return Api.deleteStatistic(payload).then(ret => {
      return ret;
    });
  },

  complexAddS({ commit }, param) {
    return Api.complexAdd({ body: param }).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_RESULTS](state, payload) {
    state.curResult = {
      complexScore: "--",
      authNum: "--",
      trainDoneRate: "--",
      taskDoneRate: "--",
      monthReadCount: "--",
      averageCount: "--",
      questCorrectRate: "--",
      newsCount: "--",
      statisticTime: "--"
    };
  },
  [types.INIT_CURRENT_STATISTIC](state, payload) {
    state.currentStatistic = {
      title: "",
      depts: []
    };
  },
  [types.UPDATE_STATISTIC_TOTAL](state, payload) {
    state.totalList = payload;
  },
  [types.UPDATE_AREA_STATISTIC_LIST](state, payload) {
    state.areaStatisList = payload;
  },
  [types.UPDATE_DEPT_REGISTER_RATE](state, payload) {
    state.deptRegisterRate = payload;
  },
  [types.UPDATE_AREA_REGISTER_RATE](state, payload) {
    state.areaRegisterRate = payload;
  },
  [types.UPDATE_DEPT_STATISTIC_LIST](state, payload) {
    state.deptStatisticList = payload;
  },
  [types.UPDATE_CUR_RESULT](state, payload) {
    state.curResult = payload;
  },
  [types.UPDATE_CURRENT_STATISTIC](state, payload) {
    state.currentStatistic = payload;
  },
  [types.UPDATE_STATISTIC_LIST](state, payload) {
    state.statisticList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
